<?php

namespace app\api\validate;

use app\h5\service\PayService;
use app\model\Money;
use think\Validate;

class Pay extends Validate
{
    protected $rule = [
        'channel'  =>  'require|inChannel',
        'total_fee' => 'require|integer|min:1',
        'name' => 'require|checkName',
        'number' => 'require|integer|min:1',
        'type' => 'require|inType',
        'video_id' => 'integer|min:1',
        'link_id' => 'integer|min:1',
    ];

    protected $message  =   [
        'channel.require'    => '支付渠道不能为空',
        'channel.inChannel'    => '支付渠道错误',
        'total_fee.require'    => '支付金额',
        'name.require'    => 'name不能为空',
        'number.require'    => 'number不能为空',
        'type.require'    => '支付类型不能为空',
        'type.inType'    => '支付类型不正确',
        'name.checkName'    => 'name传参不正确',
    ];

    protected $scene = [
        'doPay'  =>  ['channel','total_fee','name','number','type','video_id','link_id'],
    ];

    protected function inChannel($value, $rule, $data): bool
    {
        return in_array($value,array_keys(PayService::CHANNEL_ARR));
    }

    protected function inType($value, $rule, $data): bool
    {
        return in_array($value,array_keys(Money::TYPE_ARR));
    }

    protected function checkName($value, $rule, $data)
    {
        $forbiddenKeywords = ["select", "hostname", "rm", "java", "delete","union","drop"];
        foreach ($forbiddenKeywords as $keyword) {
            $pattern = "/\b" . preg_quote($keyword, '/') . "\b/i";
            if (preg_match($pattern, $value)) {
                return false;
            }
        }
        return true;
    }

}